// subPages/mine/signIn/signIn.js
import signIn from '../../../utils/signIn';
const app = getApp()
var util =  require('../../../utils/util.js');
var json = require('../../../data/json');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: util.setTime7(new Date()),
    pBtnTxt: {
      pBtnTxt: '签到'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIntegralData();
    this.getsignInData();
  },

  //获取数据
  getsignInData:function(){
    var that = this    
    wx.request({
      url: app.globalData.apiUrl +'/get_user_info',
      data: {
        userid: app.globalData.userId,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        console.log(res);
        // res.data.day //记录的总共的日期
        // res.data.qd_date //记录最后一次得日期
        // res.data.jf //积分

        //判断今天是否签到过
        let isSigned = false;
        if(res.data.qd_date){
          if(that.data.date === res.data.qd_date){
            //已签到过
            isSigned = true
            that.setData({
              btnDis: true
            })
          }
        }
        that.setPeocessIcon(res.data.day,isSigned)
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //设置签到
  setPeocessIcon: function (day,isSigned) {
    var that = this
    var processList = json.processList
    for(var i in processList){
      if(processList[i].index == day){
        if(processList[i].index > 1){
          for(var j = processList[i].index-1; j>=0; j--){
            processList[j].isCurrent = true
          }
        }else{
          processList[0].isCurrent = true
        }
        if(isSigned){
          //签到过
          processList[day-1].isSigned = true
        }
      }
    }
    that.setData({
      processList: processList,
      day: day //总签到数
    })
  },

  //获取积分
  getIntegralData:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/get_qd',
      data: {
        userid: app.globalData.userId,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res);
        that.setIntegralData(res.data)
        
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //设置积分
  setIntegralData:function(list){
    var that = this
    var processList = json.processList
    for(var i in list){
      switch (list[i].day) {
        case '1':
          if(list[i].bage){
            processList[0].isAwards = true
            processList[0].awards = list[i].bage
          }
          processList[0].integral = list[i].jf
          break;
        case '2':
          if(list[i].bage){
            processList[1].isAwards = true
            processList[1].awards = list[i].bage
          }
          processList[1].integral = list[i].jf
          break;
          case '3':
          if(list[i].bage){
            processList[2].isAwards = true
            processList[2].awards = list[i].bage
          }
          processList[2].integral = list[i].jf
          break;
          case '4':
          if(list[i].bage){
            processList[3].isAwards = true
            processList[3].awards = list[i].bage
          }
          processList[3].integral = list[i].jf
          break;
          case '5':
          if(list[i].bage){
            processList[4].isAwards = true
            processList[4].awards = list[i].bage
          }
          processList[4].integral = list[i].jf
          break;
          case '6':
          if(list[i].bage){
            processList[5].isAwards = true
            processList[5].awards = list[i].bage
          }
          processList[5].integral = list[i].jf
          break;
          case '7':
          if(list[i].bage){
            processList[6].isAwards = true
            processList[6].awards = list[i].bage
          }
          processList[6].integral = list[i].jf
          break;
      }
    }
    that.setData({
      processList: processList
    })
  },

  //签到按钮
  lickBtn:function(){
    var that = this
    var processList = that.data.processList
    wx.request({
      url: app.globalData.apiUrl +'/qd',
      data: {
        userid: app.globalData.userId,
        qd_date: that.data.date,
        jf: processList[that.data.day].integral
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        // console.log(res);
        processList[that.data.day].isCurrent = true;
        processList[that.data.day].isSigned = true;
        that.setData({
          btnDis: true,
          processList: processList,
          isBouncedAre: true,
          template: new signIn().palette(processList[that.data.day].integral)
        })
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //关闭
  close:function(){
    this.setData({
      isBouncedAre: false
    })
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})